package indi.woniu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import indi.woniu.entity.Answer;
import indi.woniu.entity.ResponseEntity;
import indi.woniu.service.AnswerService;
import indi.woniu.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    
    @RequestMapping("/findAll")
    
    public ResponseEntity findAll() {
        List<Answer> answers = answerService.findAll();
        
        answers.forEach(answer -> {
            Map<String, Object> map = null;
            try {
                map = Util.toMap(answer.getOptions());
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            answer.setMapOptions(map);
            
        });
        return new ResponseEntity(200, "成功", answers);
    }
}
